package org.walkerljl.commons.schedule;

import org.walkerljl.commons.util.NumUtils;

import java.io.Serializable;


/**
 * @author lijunlin
 */
public class Task implements Serializable {

	private static final long serialVersionUID = 8665591333960536992L;

	private Long id;
	private String name;
	private String appClass;
	/** 参数*/
	private String params;
	/** 任务类型[1.启动执行1次;2.按时间间隔循环执行;3.corn表达式;]*/
	private Integer taskType;
	/** 固定延迟时间(单位：秒)*/
	private Integer fixedDelay;
	/** 初始延迟时间(单位：秒)*/
	private Integer initialDelay;
	/**
	 * Cron表达式
	 */
	private String cronExpression;
	/** 任务状态[1:启用;0:禁用]*/
	private Integer status;
	/** 是否单实例运行[1:是;0:否]*/
	private Integer singleInstance;
	/** 实例标记*/
	private String instanceFlag;
	
	//任务计算属性
	private transient long lastExecuteTime;//最近执行时间
	private transient long nextExecuteTime;//下次执行时间
	private transient long executeCount;//执行次数
	private volatile transient boolean running;//正在运行

	public void incrementExecuteCount(long executeCount) {
		this.executeCount += executeCount;
	}

	/**
	 * 测试是否到达执行时间
	 * @return
     */
	public boolean canExecute(){
		long currentTime = System.currentTimeMillis();
		if(lastExecuteTime == 0) {lastExecuteTime = currentTime;}
		if(executeCount <= 0) {calculateNextExecuteTime();}
		boolean result = currentTime >= nextExecuteTime;
		if (result && NumUtils.equals(taskType, 1)) {
			result = executeCount <= 0;
		}
		return result;
	}

	/**
	 * 计算下次执行时间
	 */
	public void calculateNextExecuteTime(){

	}

	public boolean update(Task task) {
		return true;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAppClass() {
		return appClass;
	}

	public void setAppClass(String appClass) {
		this.appClass = appClass;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public Integer getTaskType() {
		return taskType;
	}

	public void setTaskType(Integer taskType) {
		this.taskType = taskType;
	}

	public Integer getFixedDelay() {
		return fixedDelay;
	}

	public void setFixedDelay(Integer fixedDelay) {
		this.fixedDelay = fixedDelay;
	}

	public Integer getInitialDelay() {
		return initialDelay;
	}

	public void setInitialDelay(Integer initialDelay) {
		this.initialDelay = initialDelay;
	}

	public String getCronExpression() {
		return cronExpression;
	}

	public void setCronExpression(String cronExpression) {
		this.cronExpression = cronExpression;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getSingleInstance() {
		return singleInstance;
	}

	public void setSingleInstance(Integer singleInstance) {
		this.singleInstance = singleInstance;
	}

	public String getInstanceFlag() {
		return instanceFlag;
	}

	public void setInstanceFlag(String instanceFlag) {
		this.instanceFlag = instanceFlag;
	}

	public long getLastExecuteTime() {
		return lastExecuteTime;
	}

	public void setLastExecuteTime(long lastExecuteTime) {
		this.lastExecuteTime = lastExecuteTime;
	}

	public long getNextExecuteTime() {
		return nextExecuteTime;
	}

	public void setNextExecuteTime(long nextExecuteTime) {
		this.nextExecuteTime = nextExecuteTime;
	}

	public long getExecuteCount() {
		return executeCount;
	}

	public void setExecuteCount(long executeCount) {
		this.executeCount = executeCount;
	}

	public boolean isRunning() {
		return running;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}
}
